package com.pinyougou.shop.controller;

import com.pinyougou.entity.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import utils.FastDFSClient;

/**
 * Title :文件上传
 *
 * @Project pinyougou-parent
 * @Description TODD
 * @Author 哈哈怪
 * @Date 2018/9/14 15:55
 **/
@RestController
public class UploadController {

    /**
     * 引入配置文件中图片上传服务器地址
     */
    @Value("${FILE_SERVER_URL}")
    private String FILE_SERVER_URL;

    @RequestMapping("/upload")
    public Result upload(MultipartFile file) {

        //读取文件的扩展名
        String filename = file.getOriginalFilename();
        String ext = filename.substring(filename.lastIndexOf(".") + 1);

        //创建FastDFS图片服务器客户端
        try {
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/fdfs_client.conf");

            //执行上传处理
            String path = fastDFSClient.uploadFile(file.getBytes(),ext);

            //拼接url
            String url = FILE_SERVER_URL+path;

            System.out.println(url);

            return new Result(url,true);
        } catch (Exception e) {

            e.printStackTrace();
            return new Result("上传失败",false);
        }
    }
}
